package io.gitee.yth.design.pattern.test.core.pattern.structural.proxy;

import org.junit.jupiter.api.Test;
import io.gitee.yth.design.pattern.test.core.pattern.structural.proxy.cglibproxy.CglibProxyFactory;
import io.gitee.yth.design.pattern.test.core.pattern.structural.proxy.cglibproxy.Target;
import io.gitee.yth.design.pattern.test.core.pattern.structural.proxy.jdkDynamicProxy.ProxyFactory;
import io.gitee.yth.design.pattern.test.core.pattern.structural.proxy.staticProxy.TeacherDaoProxy;

/**
 * @author yth
 * @since 2023-04-27-23:24
 */
public class Client {

    /**
     * 静态代理
     */
    @Test
    public void test1() {
        ITeacherDao proxy = new TeacherDaoProxy(new TeachDao());
        proxy.teach();
    }

    /**
     * JDK 动态代理
     */
    @Test
    public void test2() {
        TeachDao target = new TeachDao();
        ProxyFactory factory = new ProxyFactory(target);
        ITeacherDao proxyInstance = (ITeacherDao) factory.getProxyInstance();

        System.out.println(proxyInstance.getClass());

        proxyInstance.teach();
        System.out.println(proxyInstance.getName());
    }

    /**
     * CGLib 动态代理
     */
    @Test
    public void test3() {
        Target target = new Target();
        CglibProxyFactory factory = new CglibProxyFactory(target);
        Target proxyInstance = (Target) factory.getProxyInstance();

        System.out.println("----------------------");
        System.out.println(proxyInstance.getClass());

        System.out.println("----------------------");
        proxyInstance.methodA(1);

        System.out.println("----------------------");
        System.out.println("proxyInstance.methodB() = " + proxyInstance.methodB());

        System.out.println("----------------------");
        proxyInstance.methodC();

        System.out.println("----------------------");
        proxyInstance.methodD();
    }
}
